
$(function(){
	new Vue({
		el:"#shopGoodSpecApp",
		data:{
			fullscreenLoading:false,
			activeName: 'first',
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			goodLoacalStock:[],
			goodPropFrom:{},
			goodPropList:[],
			selectGoodProp:"",
			title:"添加新的商品规格",
			goodId:'',
			isAdd:false,
			fileList:[],
			categoryId:"",
			dialogFormVisible:false,
		},
		created:function(){
			parent.vueData.$data.hreHtml="../editGoodProp/index.html";
			parent.vueData.activeIndex  = "7";
			parent.vueData.getLeftNav("7")
			parent.vueData.$data.categoryIndex = 1
			this.goodId = localStorage.getItem("goodId");
			localStorage.removeItem("goodId");
			this.categoryId = localStorage.getItem("categoryId");
			localStorage.removeItem("categoryId");
			this.getAllCategoryByCommodityId()
			this.getGoodProp()
		},
		methods:{
			getGoodProp(categoryId) {
				let shopId = getShopId();
				let param = {
					shopId: shopId,
					categoryId: this.categoryId,
					admin_grade:"1",
				}
				axios({
					url: POSTURL.getCategoryList,
					method: 'post',
					data: param
				}).then(response => {
					if (response.data.code === 100) {
						console.log(response.data.result);
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.name = obj.name;
							category.select = "";
							category.options = [];
							for (let j = 0; j < obj.optionals.length; j++) {
								let data = obj.optionals[j]
								let secondCategory = new Object();
								secondCategory.name = data;
			
								category.options.push(secondCategory)
							}
							this.goodPropList.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
			
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			getAllCategoryByCommodityId(){
				axios({
					url: POSTURL.selectByCommodityId,
					method: 'post',
					data: {
						commodityId:this.goodId,
						admin_grade:"1",
					}
				}).then(response => {
					if (response.data.code === 100) {
					   this.goodLoacalStock = response.data.result
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
				
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			handleClick(tab, event) {
				console.log(tab.name);
			},
			handleSelectionChange(val) {
				this.ids = [];
				val.forEach(obj=>{
					this.ids.push(obj.uid)
				})
			},
			handleCategoryCoverExceed(files, fileList) {
				this.$message.warning("商品规格封面最多上传1张")
			},
			handleCategoryCoverSuccess(response, file, fileList) {
				if (response.code == 100) {

					this.fileList = [{
						name:"规格图片",
						url:response.result
					}]
				}
			},
		    addGoodCategory(){
		    	this.dialogFormVisible = true
				this.isAdd = true
				this.goodPropList.forEach(obj=>{
					obj.select ="";
				})
				this.goodPropFrom= {
					price:"",
					name:"",
					inventory:"999",
				};
				this.fileList = [];
				
				
				
		    },
			eidtGoodProp(val){
				this.dialogFormVisible = true
				this.isAdd = false
				this.title = "编辑商品规格";
				this.goodPropFrom = val
				console.log(val);
				this.selectGoodProp = val.combination
				this.goodPropList.forEach(obj=>{
					obj.select ="";
				})
				
				
				this.fileList = [{
					name:"规格图片",
					url:val.photo
				}]
				
			},
			// 删除商品规格
			deleteGoodProp(val){
				 this.fullscreenLoading = true;
				axios({
					url: POSTURL.deleteGoodPropById,
					method: 'post',
					data: {
						id:val.uid,
						admin_grade:"1",
						
					}
				}).then(response => {
					 this.fullscreenLoading = false;
					if (response.data.code === 100) {
					  this.$message({
					  	showClose: true,
					  	message: response.data.message,
					  	type: 'success'
					  });
					  this.getAllCategoryByCommodityId()
					} else {
						 this.fullscreenLoading = false;
						this.$message.error(response.data.message);
					}
				}).catch(error => {
				
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			
			
			configClick(){
				if(this.isAdd){
					this.addGoodProp()
				}else{
				  this.editGoodProp()	
				}
				
			},
			isNotNumber(inputData) {
				if (parseFloat(inputData).toString() == "NaN") {
					return false;
				} else {
					return true;
				}
			},
			handleRemove(file,fileList){
				let index  =  this.fileList.indexOf(file);
				if(index >-1){
					this.fileList.splice(index,1);
				}
			},
			editGoodProp(){
				
				if (!this.isNotNumber(this.goodPropFrom.price)) {
					this.$message.error('商品规格售价只能为数字');
					return;
				}
				if (!this.isNotNumber(this.goodPropFrom.inventory)) {
					this.$message.error('商品规格库存只能为数字');
					return;
				}
				if(this.fileList.length>0){
				  this.goodPropFrom.photo=this.fileList[0].url
				}else{
					this.$message.error('商品规格图片不能为空');
					return;
				}
				
				let combination= "";
				
				let combinationArry = [];
				for(let i=0; i< this.goodPropList.length;i++){
					let value = this.goodPropList[i];
					if(!stringEmpty(value.select)){
						combinationArry.push(value.select)
					}
				}
				if(combination.length>0){
					combination = combinationArry.join('+')
				}else{
					combination  = this.selectGoodProp
				}
				let uploadGoodProp={}
				uploadGoodProp.commodityId = this.goodPropFrom.commodityId
				uploadGoodProp.price = regYuanToFen(this.goodPropFrom.price,100)
				uploadGoodProp.inventory = this.goodPropFrom.inventory
				uploadGoodProp.photo = this.goodPropFrom.photo
				uploadGoodProp.combination  = combination
				uploadGoodProp.uid = this.goodPropFrom.uid
				uploadGoodProp.admin_grade="1"
				
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.updateParameterGroup,
					method: 'post',
					data: uploadGoodProp
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.dialogFormVisible = false
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						 this.getAllCategoryByCommodityId()
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
				
			},
			addGoodProp(){
				 if (!this.isNotNumber(this.goodPropFrom.price)) {
				 	this.$message.error('商品规格售价只能为数字');
				 	return;
				 }
				 if (!this.isNotNumber(this.goodPropFrom.inventory)) {
				 	this.$message.error('商品规格库存只能为数字');
				 	return;
				 }
				 if(this.fileList.length>0){
				   this.goodPropFrom.photo=this.fileList[0].url
				 }else{
				 	this.$message.error('商品规格图片不能为空');
				 	return;
				 }
				 let combination= "";
				 
				 let combinationArry = [];
				 for(let i=0; i< this.goodPropList.length;i++){
				 	let value = this.goodPropList[i];
				 	if(!stringEmpty(value.select)){
				 		combinationArry.push(value.select)
				 	}
				 }
				 combination = combinationArry.join('+')
				 if (combinationArry.length<=0){
				 	this.$message.error('请选择规格参数');
				 	return;
				 }
				 this.goodPropFrom.commodityId = this.goodId
				 let uploadGoodProp={}
				 uploadGoodProp.commodityId = this.goodPropFrom.commodityId
				 uploadGoodProp.price = regYuanToFen(this.goodPropFrom.price,100)
				 uploadGoodProp.inventory = this.goodPropFrom.inventory
				 uploadGoodProp.photo = this.goodPropFrom.photo
				 uploadGoodProp.combination  = combination
				 this.fullscreenLoading = true;
				 uploadGoodProp.admin_grade="1"
				 axios({
				 	url: POSTURL.addGoodParameterGroup,
				 	method: 'post',
				 	data: uploadGoodProp
				 }).then(response => {
				 	this.fullscreenLoading = false;
				 	if (response.data.code === 100) {
						this.dialogFormVisible = false
				 	this.$message({
				 		showClose: true,
				 		message: response.data.message,
				 		type: 'success'
				 	  });
				 	  this.getAllCategoryByCommodityId()
				 	} else {
				 		this.$message.error(response.data.message);
				 	}
				 }).catch(error => {
				 	this.fullscreenLoading = false;
				 	console.log(error);
				 	this.$message.error('服务器出错!');
				 })
			}
		}
	})
})